/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.config.content;

import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.Config;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.config.ConfigMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class AdminTab extends Composite {
	private ConfigMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private VerticalPanel panel = null;

	private boolean initial;

	private TextBox adminName;

	private PasswordTextBox password, confirm;
	
	private HashMap<String,Config> list;

	public AdminTab(ConfigMessages cons, BifangGWTServiceAsync service,
			GwtContext context) {
		this.service = service;
		this.context = context;
		this.cons = cons;

		panel = new VerticalPanel();
		panel.setWidth("100%");
		panel.setSpacing(5);

		initWidget(panel);
	}

	public void init() {

		if (!initial) {
			Grid table = new Grid(3, 2);
			table.setWidth("50%");
			table.setCellPadding(1);
			table.setCellSpacing(1);
			table.setBorderWidth(1);

			ColumnFormatter cf = table.getColumnFormatter();

			cf.setWidth(0, "40%");
			cf.setWidth(1, "60%");

			CellFormatter ff = table.getCellFormatter();
			ff.setStyleName(0, 0, "tablecol");
			ff.setStyleName(1, 0, "tablecol");
			ff.setStyleName(2, 0, "tablecol");

			table.setHTML(0, 0, cons.adminUser());
			table.setHTML(1, 0, cons.adminPassword());
			table.setHTML(2, 0, cons.confirmPassword());

			adminName = new TextBox();
			adminName.setVisibleLength(32);

			password = new PasswordTextBox();
			password.setVisibleLength(32);

			confirm = new PasswordTextBox();
			confirm.setVisibleLength(32);
			
			table.setWidget(0, 1, adminName);
			table.setWidget(1, 1, password);
			table.setWidget(2, 1, confirm);
			
			panel.add(table);
			panel.add(new HTML("&nbsp;"));
			
			Button b = new Button(cons.modify());
			b.addClickListener(new ClickListener(){

				
				public void onClick(Widget sender) {
					if(!validate()){
						return;
					}
					ArrayList<Config> param = new ArrayList<Config>();
					
					list.get(EntityConstants.CONFIG_ADMIN_PASSWORD).setValue(password.getText());
					list.get(EntityConstants.CONFIG_ADMIN_USER).setValue(adminName.getText());
					param.add(list.get(EntityConstants.CONFIG_ADMIN_PASSWORD));
					param.add(list.get(EntityConstants.CONFIG_ADMIN_USER));
					
					RequestObject req = new RequestObject();
					req
							.setAction("han.bifang.gwt.server.action.config.ModifyConfigAction");
					req.setRequest(param);
					service.call(req, cb1);
							
					
				}});
			if(PrivilegChecker.hasPrivilege(context,null))
				panel.add(b);
			
			RequestObject req = new RequestObject();
			req
					.setAction("han.bifang.gwt.server.action.config.GetConfigByTypeAction");
			req.setRequest(EntityConstants.CONFIG_TYPE_ADMIN);
			service.call(req, cb);
			initial = true;
		}
	}

	public void onLoad() {

	}

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			list = new HashMap<String, Config>();
			ArrayList<Config> c = (ArrayList<Config>) res.getResponse();
			for(Config cn : c){
				list.put(cn.getKey(), cn);
			}
			adminName.setText(list.get(EntityConstants.CONFIG_ADMIN_USER).getValue());
			password.setText(list.get(EntityConstants.CONFIG_ADMIN_PASSWORD).getValue());
			confirm.setText(list.get(EntityConstants.CONFIG_ADMIN_PASSWORD).getValue());

			
		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};
	
	private AsyncCallback<ResponseObject> cb1 = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			Window.alert(cons.modifyOk());

			
		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};
	
	
	private boolean validate() {
		if (!StringUtils.checkLenght(adminName.getText(), 1, 128)) {
			Window.alert(cons.lengthError(cons.adminUser(), "1", "128"));
			return false;
		}
		if (!StringUtils.checkLenght(password.getText(), 1, 128)) {
			Window.alert(cons.lengthError(cons.adminPassword(), "1", "128"));
			return false;
		}
		if(! password.getText().equals(confirm.getText())){
			Window.alert(cons.passwordNotMatch());
			return false;
		}
		return true;

	}

}
